import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import 'package:kq_flutter_core_widget/utils/kq_multi_click_util.dart';
import 'package:kq_flutter_core_widget/widgets/button/kq_ink_well.dart';

import '../../resources/kq_theme_colors.dart';
import '../../theme/kq_theme_manager.dart';

/// 列表通用操作栏按钮
///
/// @author 周卓
///
/// 蓝色边框，蓝色文字，淡蓝色背景，水平内边距8，垂直内边距6，字号14，圆角2
class KqOutlineButton extends StatelessWidget {
  /// 标题
  final String title;

  /// 边框颜色，默认跟随主题亮色
  final Color? borderColor;

  /// 文字颜色，默认跟随主题亮色
  final Color? textColor;

  /// 背景颜色，默认透明
  final Color? bgColor;

  /// 字体大小，默认14
  final double? fontSize;

  /// 圆角大小，默认2
  final double? radius;

  /// 边框宽度，默认1
  final double borderWidth;

  /// 垂直边距，默认8
  final double? verticalPadding;

  /// 水平边距，默认8
  final double? horizontalPadding;

  /// 最小宽度，默认71
  final double? minWidth;
  final double? minHeight;

  /// 是否置灰
  final bool disabled;

  /// 点击事件
  final Function(bool disabled)? onTap;

  /// 主要按钮
  const KqOutlineButton(
      {Key? key,
      required this.title,
      this.borderColor = KqThemeColors.bgBlue,
      this.textColor = KqThemeColors.textWhite,
      this.bgColor = KqThemeColors.bgBlue,
      this.radius,
      this.minWidth,
      this.minHeight,
      this.borderWidth = 0.5,
      this.fontSize,
      this.disabled = false,
      this.verticalPadding,
      this.horizontalPadding,
      this.onTap})
      : super(key: key);

  /// 次要按钮
  const KqOutlineButton.normal({
    super.key,
    required this.title,
    this.borderColor = KqThemeColors.bgBlue,
    this.textColor = KqThemeColors.bgBlue,
    this.bgColor,
    this.fontSize,
    this.minWidth,
    this.minHeight,
    this.radius,
    this.disabled = false,
    this.borderWidth = 0.5,
    this.verticalPadding,
    this.horizontalPadding,
    this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    var borColor = borderColor ??
        (KqThemeManager.instance.getConfig().commonConfig.mainColor ??
            KqThemeColors.textBlue);
    var txtColor = textColor ??
        (KqThemeManager.instance.getConfig().commonConfig.mainColor ??
            KqThemeColors.textBlue);
    if (disabled) {
      if (txtColor == KqThemeColors.text26) {
        // 灰色处理
        txtColor = KqThemeColors.textBF;
      } else {
        // 其余透明度40%
        txtColor = txtColor.withOpacity(0.4);
        borColor = borColor.withOpacity(0.4);
      }
    }
    return ConstrainedBox(
      constraints:
          BoxConstraints(minWidth: minWidth ?? 184.r, minHeight: minHeight ?? 0),
      child: KqInkWell(
          onTap: () {
            if (KqMultiClickUtil.isMultiClick()) {
              return;
            }
            onTap?.call(disabled);
          },
          verticalPadding: verticalPadding ?? 22.r,
          horizontalPadding: horizontalPadding ?? 36.r,
          radius: radius ?? 2.r,
          borderWidth: borderWidth,
          borderColor: borColor,
          backgroundColor: bgColor ?? Colors.transparent,
          child: Align(
            alignment: Alignment.center,
            child: Text(
              title,
              textAlign: TextAlign.center,
              style: TextStyle(
                color: txtColor,
                fontWeight: FontWeight.normal,
                fontSize: fontSize ?? 28.sp,
              ),
            ),
          )),
    );
  }
}
